<?php
// Module Body

function logo_parser_add_class(&$arry) {
	global $cfg;

	$parser = $arry[0];
	$data = $arry[1];

	if (strlen($data['ownerid']) == 10) {
		$ownerid = substr($data['ownerid'], 0, 5);
	} elseif (strlen($data['ownerid']) == 0) {
		$ownerid = '00000';
	} elseif (strlen($data['ownerid']) == 5) {
		$ownerid = $data['ownerid'];
	}

	$dirlocation = $cfg['basedir'].'/web/skin/_logomod';
	$iconname = get_property('logo', $ownerid, 'logo_mod');

	$parser->AddClass('LOGO_MOD');
	if (is_file($dirlocation.'/'.$iconname)) {
		$parser->AddVar('IMGSRC', 'skin/_logomod/'.$iconname, 'LOGO_MOD');
	} else {
		$parser->StripClass('LOGO_MOD');
	}
}

function logo_upload_reseller_image(&$data) {
	global $cfg, $T;
	$cp = $data['_cp'];
	$url = $data['_url'];
	$action = $data['_action'];
	$confirm = $data['_confirm'];
	$current_url = $data['_current_url'];
	$resellerdata = $data['_resellerdata'];
	$userdata = $data['_userdata'];

	$dirlocation = $cfg['basedir'].'/web/skin/_logomod';

	if (sizeOf($_FILES)) {
		if ($userdata['type'] != 'demo') {
			$tmpfile = $_FILES['data']['tmp_name'];
			$tmptype = $_FILES['data']['type'];
			if (is_uploaded_file($tmpfile)) {
				switch($tmptype) {
					case 'image/jpeg':
						$targetfilename = $resellerdata['id'].'.jpg';
						break;
					case 'image/gif':
						$targetfilename = $resellerdata['id'].'.gif';
						break;
					case 'image/png':
						$targetfilename = $resellerdata['id'].'.png';
						break;
					default:
						break;
				}
				echo $targetfilename;
				if ($targetfilename != '') {
					move_uploaded_file($tmpfile, $dirlocation.'/'.$targetfilename);
					set_property('logo', $targetfilename, $resellerdata['id'], 'logo_mod');
				}
			}
		} else {
			$error[] = $T['err']['demo user'];
		}
	}
	if ($action == 'delete') {
		if ($data['type'] != 'demo') {
			$logofile = $dirlocation.'/'.get_property('logo', $resellerdata['id'], 'logo_mod');
			if (is_file($logofile)) {
				unlink($logofile);
			}
			set_property('logo', '', $resellerdata['id'], 'logo_mod');
		} else {
			$error[] = $T['err']['demo user'];
		}
	}

	if (!empty($error)) {
		echo "<br>&nbsp;&nbsp;&bull; ".implode("<br>\n&nbsp;&nbsp;&bull; ",$error)."<br>\n";
	}
?>

	<div align="center">
<table border="0" cellspacing="0" cellpadding="0" class="tblbg" width=450>
	<tr>
		<td>
		<table border="0" cellspacing="1" width="100%">
			<tr>
				<td class="tbltop">&nbsp;<?=$T['menu'][$url];?>&nbsp;</td>
			</tr>
			<tr>
	          <td width="100%">
                    <table border="0" cellpadding="3" width="100%" class="content">
                    	<? $currentlogo = get_property('logo', $resellerdata['id'], 'logo_mod');
                    	if ($currentlogo != '') {
                    	?>
                      <tr>
                        <td valign="top" nowrap align="center">
                        <img src='<?='skin/_logomod/'.$currentlogo;?>'><br>
                        (<a href='<?=$current_url;?>&action=delete'><?=$T['delete'];?></a>)
                        </td>
                      </tr>
                      <? } else { ?>
                      <tr>
                        <td valign="top" nowrap align="center">
                        <?=$T['No custom logo uploaded.'];?>
                        </td>
                      </tr>
                      <? } ?>
                      <tr>
                        <td valign="top" nowrap align="center">
                        <form action="<?=$current_url;?>" enctype="multipart/form-data" method="POST">
                        <input type="hidden" name="MAX_FILE_SIZE" value="<?=$cfg['logo_max_filesize'];?>"/>
                        <input type="file" name="data">&nbsp;<input type="submit" value="<?=$T['Upload'];?>"><br>
                        <? $filesizeinfo = file_size_info($cfg['logo_max_filesize']); ?>
						(<?=$T['maximum file size'].' '.$filesizeinfo['size'].' '.$filesizeinfo['type'];?>)
                        </form>
                        </td>
                      </tr>
                    </table>
            </td>
            </tr>
        </table>
        </td>
    </tr>
</table>

<?
}

function logo_upload_server_image(&$data) {
	global $cfg, $T;
	$cp = $data['_cp'];
	$url = $data['_url'];
	$action = $data['_action'];
	$confirm = $data['_confirm'];
	$current_url = $data['_current_url'];
	$userdata = $data['_userdata'];

	$dirlocation = $cfg['basedir'].'/web/skin/_logomod';

	if (isset($_FILES['data'])) {
		if ($userdata['type'] != 'demo') {
		$tmpfile = $_FILES['data']['tmp_name']['logo'];
		$tmptype = $_FILES['data']['type']['logo'];
		if (is_uploaded_file($tmpfile)) {
			switch($tmptype) {
				case 'image/jpeg':
					$targetfilename = $resellerdata['id'].'.jpg';
					break;
				case 'image/gif':
					$targetfilename = $resellerdata['id'].'.gif';
					break;
				case 'image/png':
					$targetfilename = $resellerdata['id'].'.png';
					break;
				default:
					break;
			}

			if ($targetfilename != '') {
				move_uploaded_file($tmpfile, $dirlocation.'/'.$targetfilename);
				set_property('logo', $targetfilename, '00000', 'logo_mod');
			}
		}
		} else {
			$error[] = $T['err']['demo user'];
		}
	}
	if ($action == 'delete') {
		if ($userdata['type'] != 'demo') {
			$logofile = $dirlocation.'/'.get_property('logo', '00000', 'logo_mod');
			if (is_file($logofile)) {
				unlink($logofile);
			}
			set_property('logo', '', '00000', 'logo_mod');
		} else {
			$error[] = $T['err']['demo user'];
		}
	}

	if (!empty($error)) {
		echo "<br>&nbsp;&nbsp;&bull; ".implode("<br>\n&nbsp;&nbsp;&bull; ",$error)."<br>\n";
	}
?>
	<div align="center">
<table border="0" cellspacing="0" cellpadding="0" class="tblbg" width=450>
	<tr>
		<td>
		<table border="0" cellspacing="1" width="100%">
			<tr>
				<td class="tbltop">&nbsp;<?=$T['menu'][$url];?>&nbsp;</td>
			</tr>
			<tr>
	          <td width="100%">
                    <table border="0" cellpadding="3" width="100%" class="content">
                    	<? $currentlogo = get_property('logo', '00000', 'logo_mod');
                    	if ($currentlogo != '') {
                    	?>
                      <tr>
                        <td valign="top" nowrap align="center">
                        <img src='<?='skin/_logomod/'.$currentlogo;?>'><br>
                        (<a href='<?=$current_url;?>&action=delete'><?=$T['delete'];?></a>)
                        </td>
                      </tr>
                      <? } else { ?>
                      <tr>
                        <td valign="top" nowrap align="center">
                        <?=$T['No custom logo uploaded.'];?>
                        </td>
                      </tr>
                      <? } ?>
                      <tr>
                        <td valign="top" nowrap align="center">
                        <form action="<?=$current_url;?>" enctype="multipart/form-data" method="POST">
                        <input type="hidden" name="MAX_FILE_SIZE" value="<?=$cfg['logo_max_filesize'];?>"/>
                        <input type="file" name="data[logo]">&nbsp;<input type="submit" value="<?=$T['Upload'];?>"><br>
                        <? $filesizeinfo = file_size_info($cfg['logo_max_filesize']); ?>
						(<?=$T['maximum file size'].' '.$filesizeinfo['size'].' '.$filesizeinfo['type'];?>)
                        </form>
                        </td>
                      </tr>
                    </table>
            </td>
            </tr>
        </table>
        </td>
    </tr>
</table>

<?
}

function logo_add_menu(&$data) {
	global $menu;

	$menu['reseller'][] = 'customlogo';
	$menu['server'][] = 'customlogo';
}

function logo_add_lang(&$data) {
	global $T;
	switch($data['lang']) {
		default:
			/*
			$T['reseller']['customlogo'] = 'Custom Logo';
			$T['server']['customlogo'] = 'Custom Logo';
			*/
			$T['menu']['customlogo'] = 'Custom Logo';

			$T['Upload'] = 'Upload';
			$T['delete'] = 'delete';
			$T['No custom logo uploaded.'] = 'No custom logo uploaded.';
			$T['maximum file size'] = 'maximum file size';
			break;
	}
}

function logo_add_cfg(&$cfg) {
	$cfg['logo_max_filesize'] = 15360;
}
?>
